/**
 * 编写一个不并发/并行的 async+await测试
 * 这是一个没用的例子，因为不会执行任何东西，只是为了演示async+await的写法
 * rust-script -d tokio  17.5_tokio和future2.rs
 */
// #[macro_use]
//extern crate tokio;
use std::time::Duration;
use std::thread;
use tokio::runtime;

struct Hero {
    name: String,
    age: u32,
}
#[tokio::main]
fn main() {
    let hero = Hero {name: "孙悟空".to_string(),age: 100,};
    let ws = Hero {name: "武松".to_string(),age: 99,};
    // 并发地执行两个异步任务
    let task1 = tokio::spawn(async {
        let result = run(&hero).await;
        println!("Result from task1: {}", result);
    });

    let task2 = tokio::spawn(async {
        let result = run(&ws).await;
        println!("Result from task2: {}", result);
    });

    // 等待所有任务完成
    //let _ = task1.await;
    //let _ = task2.await;
}

async fn fight(h: &Hero) -> String {
    let temp = h.name.clone() + "正在战斗中....";
    println!("{}", temp);
    //等待5秒钟
    thread::sleep(Duration::from_secs(5));
    temp.to_string()
}
//这个方法要返回实现Debug特质的类型
async fn run(h: &Hero) -> String {
    let r = fight(&h).await;
    r
}
